package com.learn.shop.controller;


import com.github.pagehelper.PageInfo;
import com.learn.shop.constant.R;
import com.learn.shop.job.TaskJob;
import com.learn.shop.pojo.entriy.TaskHistory;
import com.learn.shop.pojo.vo.PageVo;
import com.learn.shop.service.TaskHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("task")
@Api(tags = "定时任务")
public class TaskJobController {

    @Autowired
    private TaskHistoryService taskHistoryService;

    @PostMapping("list")
    @ApiOperation("历史记录")
    public R<PageInfo<TaskHistory>> taskList(@RequestBody PageVo pageVo) {
        PageInfo<TaskHistory> list = taskHistoryService.list(pageVo);
        return R.ok(list);
    }

    @GetMapping("/job")
    @ApiOperation("开关定时任务")
    public R operationTask(@RequestParam("taskValue") String taskValue) {
        TaskHistory history = new TaskHistory();
        history.setOperation(taskValue);
        history.setOperTime(new Date());
        try {
            taskHistoryService.insert(history);
            return R.ok("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("新增失败!");
        }
    }

    /**
     * 查询最新操作
     *
     * @return
     */
    @GetMapping("/newOperation")
    @ApiOperation("最新操作记录")
    public R newOperation() {
        TaskHistory taskHistory = taskHistoryService.newOperation();
        return R.ok(taskHistory);
    }

}
